package cn.bugstack.infrastructure.adapter.repository;

import cn.bugstack.domain.activity.adapter.repository.IActivityRepository;
import cn.bugstack.domain.activity.model.entity.UserGroupBuyOrderDetailEntity;
import cn.bugstack.domain.activity.model.valobj.*;
import cn.bugstack.domain.tag.service.ITagService;
import cn.bugstack.infrastructure.dao.*;
import cn.bugstack.infrastructure.dao.po.*;

import javax.annotation.Resource;

import cn.bugstack.infrastructure.dcc.DCCService;
import cn.bugstack.infrastructure.redis.IRedisService;
import jodd.bean.BeanUtil;
import org.redisson.api.RBitSet;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author hsj
 * @date 2025/2/14 10:40
 */
@Repository
public class ActivityRepository implements IActivityRepository {


	@Resource
	private IGroupBuyActivityDao groupBuyActivityDao;

	@Resource
	private IGroupBuyDiscountDao groupBuyDiscountDao;

	@Resource
	private ISkuDao skuDao;

	@Resource
	private ISCSkuActivityDao skuActivityDao;

	@Resource
	private IRedisService redisService;

	@Resource
	private DCCService dccService;

	@Resource
	private IGroupBuyOrderListDao groupBuyOrderListDao;

	@Resource
	private IGroupBuyOrderDao groupBuyOrderDao;


	@Override
	public GroupBuyActivityDiscountVO queryGroupBuyActivityDiscountVO(Long activityId) {
        //根据SC渠道值查询配置中最新的1个有效的活动
		GroupBuyActivity groupBuyActivityRes = groupBuyActivityDao.queryValidGroupBuyActivityId(activityId);
		if (null == groupBuyActivityRes)return null;
		String discountId = groupBuyActivityRes.getDiscountId();
		GroupBuyDiscount groupBuyDiscountRes = groupBuyDiscountDao.queryGroupBuyActivityDiscountByDiscountId(discountId);
		if (null == groupBuyDiscountRes) return null;

		GroupBuyActivityDiscountVO.GroupBuyDisCount groupBuyDiscount = GroupBuyActivityDiscountVO.GroupBuyDisCount.builder()
		                                                                                                          .discountName(groupBuyDiscountRes.getDiscountName())
		                                                                                                          .discountDesc(groupBuyDiscountRes.getDiscountDesc())
		                                                                                                          .discountType(DiscountTypeEnum.get(groupBuyDiscountRes.getDiscountType()))
		                                                                                                          .marketPlan(groupBuyDiscountRes.getMarketPlan())
		                                                                                                          .marketExpr(groupBuyDiscountRes.getMarketExpr())
		                                                                                                          .tagId(groupBuyDiscountRes.getTagId())
		                                                                                                          .build();

		return GroupBuyActivityDiscountVO.builder()
		                                 .activityId(groupBuyActivityRes.getActivityId())
		                                 .activityName(groupBuyActivityRes.getActivityName())
		                                 .groupBuyDiscount(groupBuyDiscount)
		                                 .groupType(groupBuyActivityRes.getGroupType())
		                                 .takeLimitCount(groupBuyActivityRes.getTakeLimitCount())
		                                 .target(groupBuyActivityRes.getTarget())
		                                 .validTime(groupBuyActivityRes.getValidTime())
		                                 .status(groupBuyActivityRes.getStatus())
		                                 .startTime(groupBuyActivityRes.getStartTime())
		                                 .endTime(groupBuyActivityRes.getEndTime())
		                                 .tagId(groupBuyActivityRes.getTagId())
		                                 .tagScope(groupBuyActivityRes.getTagScope())
		                                 .build();


	}

	@Override
	public SkuVO querySkuByGoodsId(String goodsId) {
		Sku sku = skuDao.querySkuByGoodsId(goodsId);
		if (null == sku) return null;
		return SkuVO.builder()
		            .goodsId(sku.getGoodsId())
		            .goodsName(sku.getGoodsName())
		            .originalPrice(sku.getOriginalPrice())
		            .build();
	}

	@Override
	public SCSkuActivityVO querySCSkuActivityBySCGoodsId(String source, String channel, String goodsId) {
		 SCSkuActivity scSkuActivityReq = new SCSkuActivity();
		 scSkuActivityReq.setSource(source);
		 scSkuActivityReq.setChannel(channel);
		 scSkuActivityReq.setGoodsId(goodsId);
		 SCSkuActivity scSkuActivity = skuActivityDao.querySCSkuActivityBySCGoodsId(scSkuActivityReq);
		 if (null == scSkuActivity) return null;
		 SCSkuActivityVO vo =new SCSkuActivityVO();
		 BeanUtils.copyProperties(scSkuActivity,vo);
		 return vo;
	}

	@Override
	public boolean isTagCrowdRange(String tagId, String userId) {

		RBitSet bitSet = redisService.getBitSet(tagId);
		if (!bitSet.isExists()) return false;
		return bitSet.get(redisService.getIndexFromUserId(userId));
	}

	@Override
	public boolean downgradeSwitch() {
		return dccService.isDowngradeSwitch();
	}

	@Override
	public boolean cutRange(String userId) {
		return dccService.isCutRange(userId);
	}

	@Override
	public List<UserGroupBuyOrderDetailEntity> queryInProgressUserGroupBuyOrderDetailListByOwner(Long activityId, String userId, Integer ownerCount) {
		// 1. 根据用户ID、活动ID，查询用户参与的拼团队伍
		GroupBuyOrderList groupBuyOrderListReq = new GroupBuyOrderList();
		groupBuyOrderListReq.setActivityId(activityId);
		groupBuyOrderListReq.setUserId(userId);
		groupBuyOrderListReq.setCount(ownerCount);
		List<GroupBuyOrderList> groupBuyOrderLists = groupBuyOrderListDao.queryInProgressUserGroupBuyOrderDetailListByUserId(groupBuyOrderListReq);
		if (null == groupBuyOrderLists || groupBuyOrderLists.isEmpty()) return null;

		// 2. 过滤队伍获取 TeamId
		Set<String> teamIds = groupBuyOrderLists.stream()
				.map(GroupBuyOrderList::getTeamId)
				.filter(teamId -> teamId != null && !teamId.isEmpty()) // 过滤非空和非空字符串
				.collect(Collectors.toSet());

		// 3. 查询队伍明细，组装Map结构
		List<GroupBuyOrder> groupBuyOrders = groupBuyOrderDao.queryGroupBuyProgressByTeamIds(teamIds);
		if (null == groupBuyOrders || groupBuyOrders.isEmpty()) return null;

		Map<String, GroupBuyOrder> groupBuyOrderMap = groupBuyOrders.stream()
				.collect(Collectors.toMap(GroupBuyOrder::getTeamId, order -> order));

		// 4. 转换数据
		List<UserGroupBuyOrderDetailEntity> userGroupBuyOrderDetailEntities = new ArrayList<>();
		for (GroupBuyOrderList groupBuyOrderList : groupBuyOrderLists) {
			String teamId = groupBuyOrderList.getTeamId();
			GroupBuyOrder groupBuyOrder = groupBuyOrderMap.get(teamId);
			if (null == groupBuyOrder) continue;

			UserGroupBuyOrderDetailEntity userGroupBuyOrderDetailEntity = UserGroupBuyOrderDetailEntity.builder()
					.userId(groupBuyOrderList.getUserId())
					.teamId(groupBuyOrder.getTeamId())
					.activityId(groupBuyOrder.getActivityId())
					.targetCount(groupBuyOrder.getTargetCount())
					.completeCount(groupBuyOrder.getCompleteCount())
					.lockCount(groupBuyOrder.getLockCount())
					.validStartTime(groupBuyOrder.getValidStartTime())
					.validEndTime(groupBuyOrder.getValidEndTime())
					.outTradeNo(groupBuyOrderList.getOutTradeNo())
					.build();

			userGroupBuyOrderDetailEntities.add(userGroupBuyOrderDetailEntity);
		}

		return userGroupBuyOrderDetailEntities;
	}

	@Override
	public List<UserGroupBuyOrderDetailEntity> queryInProgressUserGroupBuyOrderDetailListByRandom(Long activityId, String userId, Integer randomCount) {
		// 1. 根据用户ID、活动ID，查询用户参与的拼团队伍
		GroupBuyOrderList groupBuyOrderListReq = new GroupBuyOrderList();
		groupBuyOrderListReq.setActivityId(activityId);
		groupBuyOrderListReq.setUserId(userId);
		groupBuyOrderListReq.setCount(randomCount * 2); // 查询2倍的量，之后其中 randomCount 数量
		List<GroupBuyOrderList> groupBuyOrderLists = groupBuyOrderListDao.queryInProgressUserGroupBuyOrderDetailListByRandom(groupBuyOrderListReq);
		if (null == groupBuyOrderLists || groupBuyOrderLists.isEmpty()) return null;

		// 判断总量是否大于 randomCount
		if (groupBuyOrderLists.size() > randomCount) {
			// 随机打乱列表
			Collections.shuffle(groupBuyOrderLists);
			// 获取前 randomCount 个元素
			groupBuyOrderLists = groupBuyOrderLists.subList(0, randomCount);
		}

		// 2. 过滤队伍获取 TeamId
		Set<String> teamIds = groupBuyOrderLists.stream()
				.map(GroupBuyOrderList::getTeamId)
				.filter(teamId -> teamId != null && !teamId.isEmpty()) // 过滤非空和非空字符串
				.collect(Collectors.toSet());

		// 3. 查询队伍明细，组装Map结构
		List<GroupBuyOrder> groupBuyOrders = groupBuyOrderDao.queryGroupBuyProgressByTeamIds(teamIds);
		if (null == groupBuyOrders || groupBuyOrders.isEmpty()) return null;

		Map<String, GroupBuyOrder> groupBuyOrderMap = groupBuyOrders.stream()
				.collect(Collectors.toMap(GroupBuyOrder::getTeamId, order -> order));

		// 4. 转换数据
		List<UserGroupBuyOrderDetailEntity> userGroupBuyOrderDetailEntities = new ArrayList<>();
		for (GroupBuyOrderList groupBuyOrderList : groupBuyOrderLists) {
			String teamId = groupBuyOrderList.getTeamId();
			GroupBuyOrder groupBuyOrder = groupBuyOrderMap.get(teamId);
			if (null == groupBuyOrder) continue;

			UserGroupBuyOrderDetailEntity userGroupBuyOrderDetailEntity = UserGroupBuyOrderDetailEntity.builder()
					.userId(groupBuyOrderList.getUserId())
					.teamId(groupBuyOrder.getTeamId())
					.activityId(groupBuyOrder.getActivityId())
					.targetCount(groupBuyOrder.getTargetCount())
					.completeCount(groupBuyOrder.getCompleteCount())
					.lockCount(groupBuyOrder.getLockCount())
					.validStartTime(groupBuyOrder.getValidStartTime())
					.validEndTime(groupBuyOrder.getValidEndTime())
					.build();

			userGroupBuyOrderDetailEntities.add(userGroupBuyOrderDetailEntity);
		}

		return userGroupBuyOrderDetailEntities;
	}

	@Override
	public TeamStatisticVO queryTeamStatisticByActivityId(Long activityId) {
		// 1. 根据活动ID查询拼团队伍
		List<GroupBuyOrderList> groupBuyOrderLists = groupBuyOrderListDao.queryInProgressUserGroupBuyOrderDetailListByActivityId(activityId);

		// 2. 过滤队伍获取 TeamId
		Set<String> teamIds = groupBuyOrderLists.stream()
				.map(GroupBuyOrderList::getTeamId)
				.filter(teamId -> teamId != null && !teamId.isEmpty()) // 过滤非空和非空字符串
				.collect(Collectors.toSet());

		// 3. 统计数据
		Integer allTeamCount = groupBuyOrderDao.queryAllTeamCount(teamIds);
		Integer allTeamCompleteCount = groupBuyOrderDao.queryAllTeamCompleteCount(teamIds);
		Integer allTeamUserCount = groupBuyOrderDao.queryAllUserCount(teamIds);

		// 4. 构建对象
		return TeamStatisticVO.builder()
				.allTeamCount(allTeamCount)
				.allTeamCompleteCount(allTeamCompleteCount)
				.allTeamUserCount(allTeamUserCount)
				.build();
	}
}
